package com.qf.bbapi.service.impl;

import com.alibaba.druid.stat.JdbcDataSourceStat;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.bbapi.dao.ShopCarDao;
import com.qf.bbapi.dto.ShopCarInfoDto;
import com.qf.bbapi.entity.ShopCar;
import com.qf.bbapi.service.intf.ShopCarService;
import com.qf.bbapi.util.StringUtil;
import com.qf.bbapi.vo.R;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author wodexinhuai
 * @create 2022-07-02-18:00
 */

@Service
public class ShopCarServiceImpl extends ServiceImpl<ShopCarDao, ShopCar> implements ShopCarService {
    @Resource
    private ShopCarDao dao;


    @Override
    public R addGoods(int token,long goodsId, int goodsNum) {
        if (token > 0 && goodsId > 0 && goodsNum > 0){
            ShopCar shopCar = new ShopCar();
            shopCar.setUid(token);
            shopCar.setGoodsId(goodsId);
            shopCar.setGoodsNum(goodsNum);
            QueryWrapper<ShopCar> wrapper = new QueryWrapper<ShopCar>().eq("goods_id", goodsId).eq("uid", token);
            ShopCar one = dao.selectOne(wrapper);
            if (one!=null){
                shopCar.setId(one.getId());
                boolean b = updateById(shopCar);
                if (b){
                    return R.ok();
                }
            }else {
                int insert = dao.insert(shopCar);
                if(insert > 0){
                    return R.ok();
                }
            }

        }
        return R.fail();

    }

    @Override
    public R addOne(int token, long goodsId) {
        if (token > 0 && goodsId > 0){
            if (dao.addOne(token,goodsId) > 0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R minusOne(int token, long goodsId) {
        if (token > 0 && goodsId > 0){
            if (dao.minusOne(token,goodsId) > 0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R delGoods(List<Long> ids) {

            boolean b = removeBatchByIds(ids);
            if (b){
                return R.ok();
            }
        return R.fail();
    }



    @Override
    public R shopCarInfo(int token) {

        if (token > 0){
            List<ShopCarInfoDto> list = dao.shopCarInfo(token);
            if (list.size() > 0){
                return R.ok(list);
            }
        }
        return R.fail("该用户购物车为空");
    }

    @Override
    public R update(Long id, Integer goodsNum) {
        int i = dao.updateGoodsNum(id, goodsNum);
        if (i>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R showPart(List<Long> ids) {

        List<ShopCarInfoDto> dtos = dao.shopPart(ids);
        if (dtos.size()>0){
            return R.ok(dtos);
        }
        return R.fail();
    }


}
